package org.standard.base.service.impl;

import java.util.List;


import org.standard.base.model.BaseUnit;
import org.standard.base.service.BaseUnitService;
import org.standard.commons.utils.DAO;
import org.standard.sys.service.SysLogService;
import org.standard.sys.service.impl.SysLogServiceImpl;
import org.standard.sys.model.SysLog;

/**
 * 单位管理，添加、修改单位并将相关操作记录存于数据库之中
 * @author wang zitao 
 *
 */

public class BaseUnitServiceImpl implements BaseUnitService {
	private DAO dao;
	private static final char SUCCESS = '1';
	private static final char FAIL = '0';

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}
	/**
	 * 修改单位数据,有记录则修改，否则直接添加
	 * 
	 * @param unit  传入的unit对象
	 * @return   已经存入数据库的unit对象
	 * @author wang zitao
	 */

	
	public char unitModify(BaseUnit unit) {
		// TODO Auto-generated method stub
		String message="";
		SysLog sysLog=new SysLog();
		char  rem;
		if (searchRecord()==null) {  //不存在则直接添加
			try{
			String unitId = (String) dao.save(unit);
			return SUCCESS;
			}catch(Exception e)
			{
				return FAIL;
			}
			} else {
			try{
			dao.updateBulk("update BaseUnit set unitName=?,unitTel=?,unitFax=?,	unitPost=?,	unitAddress=?," +
					"unitUrl=?,unitEmail=?,unitBank=?,unitBankNo=? where 1=1", new Object[]{unit.getUnitName(),
					unit.getUnitTel(),unit.getUnitFax(),unit.getUnitPost(),unit.getUnitAddress(),unit.getUnitUrl(),
					unit.getUnitEmail(),unit.getUnitBank(),unit.getUnitBankNo()});
				return SUCCESS;
			}catch(Exception e)
			{
                return FAIL;
			}
		}
	}

	/**
	 * 查询单位表中的所有记录
	 * @return 如果存在记录则返回该记录；否则的话返回值为null
	 * @author wang zitao
	 */
	public BaseUnit searchRecord() {
		boolean s = false;
		BaseUnit unit=null;

		String hql = " from BaseUnit where 1=1";

		List list = dao.find(hql);

		if (list.size() > 0) {
			unit=(BaseUnit)list.get(0);
		}
		return unit;
	}
}
